#include "inputCommand.h"
#include <wiringSerial.h>
#include <wiringPi.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include "voice.h"


// uartTool
void serialSendstring(const int fd, const unsigned char *s, int len)
{
    int ret;
    ret = write(fd, s, len);
    if (ret < 0)
        printf("Serial Puts Error\n");
}


int voiceInit(struct inputCommand* command,char *name ,char *ipAddress, int port);
int voiceGetCommand(struct inputCommand* command,char *cmd);

struct inputCommand voiceCommand = {
    .commandName = "voiceDevice",
    .command = {'\0'},
    .deviceName = "/dev/ttyAMA0",
    .Init = voiceInit,
    .getCommand = voiceGetCommand,
    .log = {'\0'}
};

int voiceInit(struct inputCommand* command,char *name ,char *ipAddress, int port)
{
    int fd;
    
    if((fd = serialOpen(command->deviceName,port)) == -1)
    {
        printf("serialOpen error\n");
        exit(-1);
    }
    command->fd = fd;
}

int voiceGetCommand(struct inputCommand* command,char *cmd)
{
    int nread = 0;
    memset(command->command,'\0',sizeof(command->command));
    nread = read(command->fd,command->command,sizeof(command->command));
    
    return nread;
}

//语音播报
void *voice_set(void *arg)
{
    pthread_detach(pthread_self());
    unsigned char *buffer = (unsigned char *)arg;
    
     if (-1 == voiceCommand.fd)
    {
        pthread_exit(0);
    }
    
    if (NULL != buffer)
    {
        printf("send:%x,%x\r\n",buffer[2],buffer[3]);
        serialSendstring(voiceCommand.fd, buffer, 6); // 09 08 0C 00 08 09 打开成功  09 08 0D 00 08 09打开失败
    }
    pthread_exit(0); 
}


struct inputCommand* addVoiceToInputCommandLink(struct inputCommand* phead)
{
    if(phead == NULL)
    {
        return &voiceCommand;
    }
    else
    {
        voiceCommand.Next = phead;
        phead = &voiceCommand;
        return phead;
    }

}


